package com.example.movie.ui.user.fragment

import android.content.Context
import android.content.pm.ActivityInfo
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.View.OnClickListener
import com.dueeeke.videocontroller.StandardVideoController
import com.dueeeke.videocontroller.component.VodControlView
import com.dueeeke.videoplayer.controller.MediaPlayerControl
import com.dueeeke.videoplayer.player.VideoView
import com.example.movie.R

class PortraitWhenFullScreenController @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : StandardVideoController(context, attrs, defStyleAttr) {

    private lateinit var mFullScreen: View

    override fun initView() {
        super.initView()
        val vodControlView = VodControlView(context)
        vodControlView.showBottomProgress(false)
        mFullScreen = vodControlView.findViewById(R.id.fullscreen)
        mFullScreen.setOnClickListener(OnClickListener { toggleFullScreen() })
        addControlComponent(vodControlView)
    }

    override fun setMediaPlayer(mediaPlayer: MediaPlayerControl) {
        super.setMediaPlayer(mediaPlayer)
        //不监听设备方向
        mOrientationHelper.setOnOrientationChangeListener(null)
    }

    override fun toggleFullScreen() {
        if (mActivity == null) return
        val o = mActivity!!.requestedOrientation
        if (o == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            mActivity!!.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else {
            mActivity!!.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
        mFullScreen!!.isSelected = o != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        adjustView()
    }

    override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
        if (!mControlWrapper.isFullScreen) {
            mControlWrapper.startFullScreen()
            return true
        }
        mControlWrapper.toggleShowState()
        return true
    }

    override fun onPlayerStateChanged(playerState: Int) {
        super.onPlayerStateChanged(playerState)
        if (playerState == VideoView.PLAYER_FULL_SCREEN) {
            mFullScreen!!.isSelected = false
        } else {
            hide()
        }
        adjustView()
    }

    private fun adjustView() {
        if (mActivity != null && hasCutout()) {
            val orientation = mActivity!!.requestedOrientation
            val cutoutHeight = cutoutHeight
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                setPadding(0, cutoutHeight, 0, 0)
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                setPadding(cutoutHeight, 0, 0, 0)
            }
        }
    }

    override fun onClick(v: View) {
        val i = v.id
        if (i == R.id.fullscreen) {
            toggleFullScreen()
        } else if (i == R.id.lock) {
            mControlWrapper.toggleLockState()
        } else if (i == R.id.iv_play) {
            togglePlay()
        } else if (i == R.id.back) {
            stopFullScreen()
        } else if (i == R.id.thumb) {
            mControlWrapper.start()
            mControlWrapper.startFullScreen()
        } else if (i == R.id.iv_replay) {
            mControlWrapper.replay(true)
            mControlWrapper.startFullScreen()
        }
    }
}